package com.xique.door.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xique.common.core.utils.DateUtils;
import com.xique.door.bean.domain.PermissionGroupDevice;
import com.xique.door.bean.request.permissionGroup.PermissionGroupDeviceRequest;
import com.xique.door.bean.search.PermissionGroupDeviceListSearch;
import com.xique.door.mapper.PermissionGroupDeviceMapper;
import com.xique.door.service.IPermissionGroupDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 权限组设备Service业务层处理
 *
 * @author ww
 * @date 2021-06-17
 */
@Service
public class PermissionGroupDeviceServiceImpl implements IPermissionGroupDeviceService
{
    @Autowired
    private PermissionGroupDeviceMapper permissionGroupDeviceMapper;

    /**
     * 查看权限组设备列表
     *
     * @param groupIds
     * @return
     */
    @Override
    public List<PermissionGroupDevice> selectPermissionGroupDeviceListByGroupIds(List<Long> groupIds) {
        if (groupIds.isEmpty()) {
            return new ArrayList<>();
        }
        return permissionGroupDeviceMapper.selectPermissionGroupDeviceListByGroupIds(groupIds);
    }

    /**
     * 获取权限组设备列表
     * @author shd
     * @date 2021/7/6 11:36
     * @param deviceIds 设备IDS
     * @return java.util.List<com.jzy.oms.access.permissionGroup.device.bean.domain.PermissionGroupDevice>
     */
    @Override
    public List<PermissionGroupDevice> selectPermissionGroupDeviceListByDeviceIds(List<Long> deviceIds) {
        if (deviceIds.isEmpty()) {
            return new ArrayList<>();
        }
        return permissionGroupDeviceMapper.selectPermissionGroupDeviceListByDeviceIds(deviceIds);
    }

    /**
     * 批量插入权限组设备信息
     * @author shd
     * @date 2021/7/6 10:15
     * @param list
     * @return int
     */
    @Override
    public int batchInsertPermissionGroupDevice(List<PermissionGroupDevice> list) {
        if (list.isEmpty()) {
            return 1;
        }
        return permissionGroupDeviceMapper.batchInsertPermissionGroupDevice(list);
    }

    @Override
    public int batchDeletePermissionGroupDevice(List<Long> ids) {
        if (ids.isEmpty()) {
            return 1;
        }
        return permissionGroupDeviceMapper.deletePermissionGroupDeviceByIds(ids);
    }

    @Override
    public List<PermissionGroupDevice> selectPermissionGroupDeviceListBySearch(PermissionGroupDeviceListSearch search) {
        if (search.getGroupId() == null && search.getGroupIds().isEmpty() &&
            search.getDeviceId() == null && search.getDeviceIds().isEmpty()) {
            return new ArrayList<>();
        }

        return permissionGroupDeviceMapper.selectPermissionGroupDeviceListBySearch(search);
    }

    @Override
    public List<PermissionGroupDevice> selectUnallocatedList(PermissionGroupDeviceRequest request) {
        return permissionGroupDeviceMapper.selectUnallocatedList(request);
    }

    @Override
    public List<PermissionGroupDevice> selectAllocatedList(PermissionGroupDeviceRequest request) {
        return permissionGroupDeviceMapper.selectAllocatedList(request);
    }
    /**
     * 查询权限组设备
     *
     * @param id 权限组设备ID
     * @return 权限组设备
     */
    @Override
    public PermissionGroupDevice selectPermissionGroupDeviceById(Long id)
    {
        return permissionGroupDeviceMapper.selectById(id);
    }

    /**
     * 查询权限组设备列表
     *
     * @param permissionGroupDevice 权限组设备
     * @return 权限组设备
     */
    @Override
    public List<PermissionGroupDevice> selectPermissionGroupDeviceList(PermissionGroupDevice permissionGroupDevice)
    {
        QueryWrapper<PermissionGroupDevice> wrapper = new QueryWrapper<>();
        if (null != permissionGroupDevice.getGroupId()) {
            wrapper.eq("group_id", permissionGroupDevice.getGroupId());
        }
        return permissionGroupDeviceMapper.selectList(wrapper);
    }

    /**
     * 新增权限组设备
     *
     * @param permissionGroupDevice 权限组设备
     * @return 结果
     */
    @Override
    public int insertPermissionGroupDevice(PermissionGroupDevice permissionGroupDevice)
    {
        permissionGroupDevice.setCreateTime(DateUtils.getNowDate());
        return permissionGroupDeviceMapper.insert(permissionGroupDevice);
    }

    /**
     * 修改权限组设备
     *
     * @param permissionGroupDevice 权限组设备
     * @return 结果
     */
    @Override
    public int updatePermissionGroupDevice(PermissionGroupDevice permissionGroupDevice)
    {
        permissionGroupDevice.setUpdateTime(DateUtils.getNowDate());
        return permissionGroupDeviceMapper.updateById(permissionGroupDevice);
    }

    /**
     * 批量删除权限组设备
     *
     * @param ids 需要删除的权限组设备ID
     * @return 结果
     */
    @Override
    public int deletePermissionGroupDeviceByIds(Long[] ids)
    {
        return permissionGroupDeviceMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除权限组设备信息
     *
     * @param id 权限组设备ID
     * @return 结果
     */
    @Override
    public int deletePermissionGroupDeviceById(Long id)
    {
        return permissionGroupDeviceMapper.deleteById(id);
    }



}